package com.gitee.wsl.common.ui.draw.node

import androidx.annotation.FloatRange
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.BlendMode
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.graphics.drawscope.DrawScope
import androidx.compose.ui.graphics.drawscope.DrawScope.Companion.DefaultBlendMode
import androidx.compose.ui.graphics.drawscope.DrawStyle
import androidx.compose.ui.graphics.drawscope.Fill
import androidx.compose.ui.graphics.drawscope.Stroke
import androidx.compose.ui.unit.dp
import com.gitee.wsl.common.ui.base.ColorOrBrush
import com.gitee.wsl.common.ui.base.Point
import com.gitee.wsl.common.ui.draw.api.HasFill
import com.gitee.wsl.common.ui.draw.api.HasStroke
import com.gitee.wsl.common.ui.draw.api.HasTransform
import com.gitee.wsl.common.ui.draw.api.Transform
import com.gitee.wsl.common.ui.draw.container.GroupNode
import com.gitee.wsl.common.ui.draw.style.toStroke
import com.gitee.wsl.mathematics.geometry.shape.Circle

class CircleNode(
     val node: Circle
) : AbstractNode(),
     HasStroke,
     HasTransform,
     HasFill {

    override var transform: Transform? = null

    /*override fun DrawScope.draw() {
        with(node) {
            val r = radius.dp.toPx()
            val c = Offset(centerX.dp.toPx(), centerY.dp.toPx())
            fill?.let {
                when (it) {
                    is ColorOrBrush.Color -> drawCircle(
                        color = it.value,
                        radius = r,
                        center = c
                    )
                    is ColorOrBrush.Brush -> drawCircle(
                        brush = it.value,
                        radius = r,
                        center = c
                    )
                }
            }

            strokeColor?.let {
                val stroke = strokeWidth?.toStroke(this@draw) ?: Stroke()
                when (it) {
                    is ColorOrBrush.Color -> drawCircle(
                        color = it.value,
                        radius = r,
                        center = c,
                        style = stroke
                    )
                    is ColorOrBrush.Brush -> drawCircle(
                        brush = it.value,
                        radius = r,
                        center = c,
                        style = stroke
                    )
                }
            }
        }
    }*/

    override fun DrawScope.drawOutLine(colorOrBrush: ColorOrBrush) {
        with(node) {
            val r = radius.dp.toPx()
            val c = Offset(centerX.dp.toPx(), centerY.dp.toPx())
            drawCircle(
                colorOrBrush = colorOrBrush,
                radius = r,
                center = c
            )
        }
    }

    override fun DrawScope.drawFill(colorOrBrush: ColorOrBrush) {
        with(node) {
            val r = radius.dp.toPx()
            val c = Offset(centerX.dp.toPx(), centerY.dp.toPx())
            val stroke = strokeWidth?.toStroke(this@drawFill) ?: Stroke()
            drawCircle(
                colorOrBrush = colorOrBrush,
                radius = r,
                center = c,
                style = stroke
            )
        }
    }

}


fun GroupNode.circle(position: Point , radius: Float = 0.0f, block: CircleNode.(GroupNode)->Unit){
    val node = CircleNode(Circle(position, radius))
    node.block(this)
    add(node)
}


fun DrawScope.drawCircle(
    colorOrBrush: ColorOrBrush,
    radius: Float = size.minDimension / 2.0f,
    center: Offset = this.center,
    @FloatRange(from = 0.0, to = 1.0) alpha: Float = 1.0f,
    style: DrawStyle = Fill,
    colorFilter: ColorFilter? = null,
    blendMode: BlendMode = DefaultBlendMode
){
    when (colorOrBrush) {
        is ColorOrBrush.Color -> drawCircle(
            color = colorOrBrush.value,
            radius = radius,
            center = center,
            style = style,
            colorFilter = colorFilter,
            blendMode = blendMode
        )
        is ColorOrBrush.Brush -> drawCircle(
            brush = colorOrBrush.value,
            radius = radius,
            center = center,
            style = style,
            colorFilter = colorFilter,
            blendMode = blendMode
        )
    }
}